package com.burgeon.backend.service.Impl;

import com.burgeon.backend.entity.TemplateSwitch;
import com.burgeon.backend.mappers.TemplateMapper;
import com.burgeon.backend.service.ActivityService;
import com.burgeon.backend.service.ex.ErrorDataException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ActivityServiceImpl implements ActivityService {
    private final static Log logger = LogFactory.getLog(ActivityService.class);

    @Autowired
    private TemplateMapper templateMapper;

    @Override
    public TemplateSwitch GetSwitchData(String account) {
        TemplateSwitch templateSwitch = templateMapper.FindTemplateSwitch(account);
        templateSwitch.setSwitch1(templateSwitch.getSwitch1().equals("Y") ? "1" : "2");
        templateSwitch.setSwitch2(templateSwitch.getSwitch2().equals("Y") ? "1" : "2");
        templateSwitch.setSwitch3(templateSwitch.getSwitch3().equals("Y") ? "1" : "2");
        templateSwitch.setSwitch4(templateSwitch.getSwitch4().equals("Y") ? "1" : "2");
        templateSwitch.setSwitch6(templateSwitch.getSwitch6().equals("Y") ? "1" : "2");
        templateSwitch.setSwitch7(templateSwitch.getSwitch7().equals("Y") ? "1" : "2");
        templateSwitch.setSwitch8(templateSwitch.getSwitch8().equals("Y") ? "1" : "2");
        templateSwitch.setSwitch9(templateSwitch.getSwitch9().equals("Y") ? "1" : "2");
        templateSwitch.setSwitch10(templateSwitch.getSwitch10().equals("Y") ? "1" : "2");
        templateSwitch.setSelectEmp(templateMapper.findselectEmp(account));
        templateSwitch.setSign(templateMapper.findSign(account));

        return templateSwitch;
    }

    @Override
    public void UpDataSwitch(TemplateSwitch templateSwitch) throws ErrorDataException {
        if (null == templateSwitch.getAccount()) {
            return;
        }
        //判断为空
        if (templateSwitch.getSelectEmp()==null||"".equals(templateSwitch.getSelectEmp())){
            templateSwitch.setSelectEmp("0");
        }
        if (null == templateMapper.findselectEmp(templateSwitch.getToken())) {
            //新建记录
            templateMapper.createSelectEmp(templateSwitch.getSelectEmp(), templateSwitch.getToken());
        } else {
            //更改记录
            templateMapper.upSelectEmp(templateSwitch.getSelectEmp(), templateSwitch.getToken());
        }
        if (templateSwitch.getSign()==null||"".equals(templateSwitch.getSign())){
            templateSwitch.setSign("0");
        }
        if (null == templateMapper.findSign(templateSwitch.getToken())) {
            //新建记录
            templateMapper.createSignData(templateSwitch.getSign(), templateSwitch.getToken());
        } else {
            //更改记录
            templateMapper.upSignData(templateSwitch.getSign(), templateSwitch.getToken());
        }


        if (templateMapper.FindTemplateSwitch(templateSwitch.getAccount()) == null) {
            try {
                templateMapper.NewTemplateSwitch(templateSwitch);
            } catch (Exception e) {
                logger.error(e.getStackTrace());
                throw new ErrorDataException("创建失败");
            }
            return;
        }
        try {
            templateMapper.UpDataTemplateSwitch(templateSwitch);
        } catch (Exception e) {
            logger.error(e.getStackTrace());
            throw new ErrorDataException("修改错误");
        }

    }
}
